package com.zmn.plat.admin.controller.channel.sp;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.admin.controller.channel.ChannelBaseController;
import com.zmn.plat.business.interfaces.channel.ChannelSpBService;
import com.zmn.plat.common.dictionary.ChannelSpBlackListTypeEnum;
import com.zmn.plat.services.interfaces.channel.sp.ChannelSpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 渠道管理新增服务商配置
 *
 * @author jiafei
 * @version 1.0
 * @since 2021/6/9 14:35
 */
@Slf4j
@RestController
@RequestMapping("/channel/sp/")
public class ChannelSpModifyController extends ChannelBaseController {

    private static final String TAG = "渠道服务商基础服务接口";

    @Resource
    private ChannelSpService channelSpService;

    @Resource
    private ChannelSpBService channelSpBService;

    /**
     * 描述: 保存
     *
     * @author guoqiao
     * @since 2021-06-09 16:50:59
     */
    @RequestMapping("/add")
    public ResponseDTO<List<Integer>> add(@RequestParam(value = "channelId") Integer channelId,
                                   @RequestParam(value = "spIds") List<Integer> spIds,
                                   @RequestParam(value = "type") Integer type) {

        log.info("[{}]:ChannelSpModifyController.add参数channelId：{}，spIds：{}, type:{}", TAG, channelId, spIds, type);
        if (NumberUtil.isNullOrZero(channelId) || CollectionUtil.isNullOrEmpty(spIds)
                || Objects.isNull(ChannelSpBlackListTypeEnum.of(type))) {
            log.error("ChannelSpModifyController.add 无效的服务商数据channelId：{}，spIds：{}, type:{}", channelId, spIds, type);
            return ResponseDTO.fail("无效的服务商数据");
        }
        return channelSpBService.add(channelId, spIds, type, getMcStaffRealName());
    }

    /**
     * 描述: 删除
     *
     * @author jiafei
     * @since 2021-06-09 16:50:59
     */
    @RequestMapping(value = "/del")
    public ResponseDTO<List<Integer>> delete(@RequestParam(value = "ids") List<Integer> ids) {

        log.info("[{}]:ChannelSpModifyController.delete 参数 ids：{}", TAG, ids);
        try {
            if (CollectionUtil.isNullOrEmpty(ids)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            channelSpService.deleteBatchByKeys(ids);
            return ResponseDTO.success(ids, "删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }
}
